package com.cysyz.web.modules.base.support.controller;


import com.cysyz.json.fastjson.JSONGeneratorFastJson;
import com.cysyz.json.framework.IJsonConfig;
import com.cysyz.json.framework.IJsonGenerator;
import com.cysyz.json.framework.ResultType;
import com.cysyz.mylib.utils.MyReflectionUtils;
import com.cysyz.mylib.utils.myassert.MyAssert;
import com.cysyz.mylib.utils.strings.wraper.StringWrapper;
import com.cysyz.web.modules.base.support.ServletActionContext;
import com.cysyz.web.modules.base.support.SysConstants;
import com.cysyz.web.modules.base.user.domain.SysUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cuiyong on 2014/12/15.
 */
public abstract class BaseController {
    protected String responseJSON(List<Object> returnContent, IJsonConfig jsonConfig, ResultType resultType, StringWrapper jsonWrapper) {
        IJsonGenerator jsonGenerator = new JSONGeneratorFastJson(jsonWrapper);

        String result = jsonGenerator.generateJSON(resultType, returnContent, jsonConfig);

        return result;
    }

    protected String responseJSON(List<Object> returnContent, ResultType resultType) {

        return this.responseJSON(returnContent, null, resultType, null);
    }

    protected String responseJSON(List<Object> returnContent) {

        return this.responseJSON(returnContent, null, ResultType.SUCCESS, null);
    }

    protected void setBusineeLogMessage(String msg) {
        ServletActionContext.getContext().put(SysConstants.BusinessLog.BUSINESSLOG_MESSAGE, msg);
    }

    protected String responseJSON(Object... returnContent) {
        List<Object> contents = new ArrayList<Object>();
        for (Object content : returnContent) {
            contents.add(content);
        }
        return responseJSON(contents);
    }

    protected String responseOriginalJSON(IJsonConfig jsonConfig, Object returnContent) {
        IJsonGenerator jsonGenerator = new JSONGeneratorFastJson();

        String result = jsonGenerator.generateJSON(returnContent, jsonConfig);
        return result;
    }

    protected String responseOriginalJSON(Object returnContent) {
        return this.responseOriginalJSON(null, returnContent);
    }

    protected String responseJSON(StringWrapper jsonWraper, Object... returnContent) {
        List<Object> contents = new ArrayList<Object>();
        for (Object content : returnContent) {
            contents.add(content);
        }
        return responseJSON(contents, null, ResultType.SUCCESS, jsonWraper);
    }

    protected String responseJSON(IJsonConfig jsonConfig, Object... returnContent) {
        List<Object> contents = new ArrayList<Object>();
        for (Object content : returnContent) {
            contents.add(content);
        }

        return this.responseJSON(contents, jsonConfig, ResultType.SUCCESS, null);
    }

    protected String responseJSON(IJsonConfig jsonConfig, StringWrapper jsonWraper, Object... returnContent) {
        List<Object> contents = new ArrayList<Object>();
        for (Object content : returnContent) {
            contents.add(content);
        }
        return responseJSON(contents, jsonConfig, ResultType.SUCCESS, jsonWraper);
    }

    public String getMethod() {
        return getRequest().getMethod();
    }

    public boolean isPost() {
        return getMethod().equalsIgnoreCase("post");
    }

    public boolean isGet() {
        return getMethod().equalsIgnoreCase("get");
    }

    public HttpSession getSession() {
        return this.getRequest().getSession();
    }

    public PrintWriter getWriter() throws IOException {
        return getResponse().getWriter();
    }

    public <T> T getSessionAttribute(Class<T> clazz, String key) {
        return (T) getSession().getAttribute(key);
    }

    public void setSessionAttribute(String key, Object value) {
        getSession().setAttribute(key, value);
    }
    public String getRealPath(String path) {
        return getRequest().getServletContext().getRealPath(path);
    }

    public void setRequestAttribute(String key, Object value) {
        this.getRequest().setAttribute(key, value);
    }

    protected String transcodingIfGet(String val) {
        MyAssert.notNull(val);
        String retVal = val;
        if (isGet()) {
            try {
                retVal = new String(retVal.getBytes("iso-8859-1"), "utf-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
        return retVal;
    }

    public void transcodingIfGet(Object obj, String... fieldNames) {
        for (String fieldName : fieldNames) {
            Object fieldValue = MyReflectionUtils.getFieldValue(fieldName, obj);
            if (fieldValue instanceof String) {
                MyReflectionUtils.setFieldValue(obj, fieldName, transcodingIfGet(fieldValue.toString()));
            }
        }
    }

    public abstract HttpServletRequest getRequest();

    public abstract HttpServletResponse getResponse();

    protected SysUser getCurrentLoginSysuser() {
        return getSessionAttribute(SysUser.class, SysConstants.CURRENT_LOGIN_SYSUSER);
    }

    protected void setCurrentLoginSysuser(SysUser sysUser) {
        setSessionAttribute(SysConstants.CURRENT_LOGIN_SYSUSER, sysUser);
    }

    protected void refreshCurrentUser(SysUser sysUser) {
        if(getCurrentLoginSysuser().equals(sysUser)) {
            setCurrentLoginSysuser(sysUser);
        }
    }
}
